/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.common;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.checkFlow.db.dao.BaseDao;
import com.yunhoo.checkFlow.model.CheckTree;
import com.yunhoo.checkFlow.model.Level;
import com.yunhoo.sys.db.config.DaoConfig;
//import com.yunhoo.system.util.err.DocumentQueryConditionBean;
//import com.yunhoo.system.util.err.ErrMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class Util {

    public BaseDao type;

    public Util() {
        DaoManager daoManager = DaoConfig.getDaoManager();
        type = (BaseDao) daoManager.getDao(BaseDao.class);
    }

    public String check(String number, String ordername, String writer, List<Level> checknodes, Level currennode) {
        String flowMark = "";
        List checkers = new ArrayList<String>();
        String fromp = LoginParam.getPer().getCpsn_name();
        String message = fromp + "审批了单据号为" + number + "的" + ordername;
        String title = "单据审批";
//        Date now = SysAdminTool.loginDate();
        Date now = new Date();
        // DocumentQueryConditionBean dqcb = (DocumentQueryConditionBean) FacesUtils.getManagedBean("queryBean");
        int totLevel = checknodes.size();
        //检查上级是否审核
        if (currennode.getLevel() == 1 || (currennode.getLevel() != 1 && checknodes.get(currennode.getLevel() - 2).getIsCheck().equals("1") && checknodes.get(currennode.getLevel() - 2).getIsConfirm().equals("1"))) {
            currennode.setCheckdate(now);
            currennode.setIsCheck("1");
            currennode.setNumber(number);
            if (currennode.getIsConfirm().equals("1")) {
                String logincode = LoginParam.getPer().getCpsn_number();
                List<CheckTree> temp = currennode.getNodes();
                if (temp != null && temp.size() > 0) {
                    for (int k = 0; k < temp.size(); k++) {
                        if (temp.get(k).getId().equals(logincode)) {
                            currennode.getChecker().setCpsn_number(logincode);
                            currennode.getChecker().setCpsn_name(fromp);
                            currennode.setId(temp.get(k).getFid());
                            type.insert(currennode, "flowinfo");
                        }
                    }
                }
                //单据审批完，检查是否有下一结点，有则提交单据到下一结点，没有则改变单据状态
                if (currennode.getLevel() == 1 && totLevel == 1) {
                    System.out.println(writer);
                    if (!writer.equals(fromp) && !checkers.contains(writer)) {
                        checkers.add(writer);
                    }
                    //修改订单状态为完成状态
                    flowMark = "4";
                }
                if (currennode.getLevel() == 1 && totLevel != 1) {
                    flowMark = "1";
                    //修改订单状态为完成状态
                    if (!writer.equals(fromp) && !checkers.contains(writer)) {
                        checkers.add(writer);
                    }
                    List<CheckTree> tempchecker = checknodes.get(currennode.getLevel()).getNodes();
                    if (tempchecker != null && tempchecker.size() > 0) {
                        for (int t = 0; t < tempchecker.size(); t++) {
                            if (!tempchecker.get(t).getName().equals(fromp) && !checkers.contains(tempchecker.get(t).getName())) {
                                checkers.add(tempchecker.get(t).getName());
                            }
                        }
                    }
                }
                if (currennode.getLevel() == totLevel && totLevel != 1) {
                    //是最后一个，且不止一级审批
                    //修改订单状态为完成状态
                    flowMark = "4";
                    List<CheckTree> tempchecker = checknodes.get(currennode.getLevel() - 2).getNodes();
                    if (tempchecker != null && tempchecker.size() > 0) {
                        for (int t = 0; t < tempchecker.size(); t++) {
                            if (!tempchecker.get(t).getName().equals(fromp) && !checkers.contains(tempchecker.get(t).getName())) {
                                checkers.add(tempchecker.get(t).getName());
                            }
                        }
                    }
                }
                if (currennode.getLevel() != totLevel && currennode.getLevel() != 1) {
                    flowMark = "1";
                    if (checknodes.get(currennode.getLevel() - 2).getChecker().getCpsn_name().equals(checknodes.get(currennode.getLevel()).getChecker().getCpsn_name())) {
                        List<CheckTree> tempchecker = checknodes.get(currennode.getLevel()).getNodes();
                        if (tempchecker != null && tempchecker.size() > 0) {
                            for (int t = 0; t < tempchecker.size(); t++) {
                                if (!tempchecker.get(t).getName().equals(fromp) && !checkers.contains(tempchecker.get(t).getName())) {
                                    checkers.add(tempchecker.get(t).getName());
                                }
                            }
                        }
                    } else {
                        List<CheckTree> tempchecker = checknodes.get(currennode.getLevel() - 2).getNodes();
                        if (tempchecker != null && tempchecker.size() > 0) {
                            for (int t = 0; t < tempchecker.size(); t++) {
                                if (!tempchecker.get(t).getName().equals(fromp) && !checkers.contains(tempchecker.get(t).getName())) {
                                    checkers.add(tempchecker.get(t).getName());
                                }
                            }
                        }
                        tempchecker = checknodes.get(currennode.getLevel()).getNodes();
                        if (tempchecker != null && tempchecker.size() > 0) {
                            for (int t = 0; t < tempchecker.size(); t++) {
                                if (!tempchecker.get(t).getName().equals(fromp) && !checkers.contains(tempchecker.get(t).getName())) {
                                    checkers.add(tempchecker.get(t).getName());
                                }
                            }
                        }
                    }
                }
            } else {
                type.delete01(currennode, "flowinfo");
                if (checknodes != null && checknodes.size() > 0) {
                    for (int i = 0; i < checknodes.size(); i++) {
                        checknodes.get(i).setIsCheck("0");
                    }
                }
                flowMark = "2";
                //单据打回，需修改后重新提交
                title = "单据退回";
                message = fromp + "退回单据号为" + number + "的" + ordername + ",原因为：" + currennode.getContent() + "，请修改后重新提交";
                if (!writer.equals(fromp) && !checkers.contains(writer)) {
                    checkers.add(writer);
                }
            }
            //dqcb.setErr(new ErrMessage("审核成功"));
            ToolKit.msgs_err("审核成功");
        } else {
            //dqcb.setErr(new ErrMessage("前一级未审核完"));
            ToolKit.msgs_err("前一级未审核完");
        }
        //sendMessage(fromp, checkers, title, message);
        return flowMark;
    }

    public void cancelcheck(String number, String ordername, String writer, List<Level> checknodes, Level currennode) {
        List checkers = new ArrayList<String>();
        String fromp = LoginParam.getPer().getCpsn_name();
        String message = fromp + "弃审了单据号为" + number + "的" + ordername;
        String title = "单据弃审";
        //DocumentQueryConditionBean dqcb = (DocumentQueryConditionBean) FacesUtils.getManagedBean("queryBean");
        //判断下一级是否已审核
        int totLevel = checknodes.size();
        if (currennode.getLevel() == totLevel || (currennode.getLevel() < totLevel && checknodes.get(currennode.getLevel()).getIsCheck().equals("0"))) {
            currennode.setCheckdate(null);
            currennode.setIsCheck("0");
            currennode.setIsConfirm("0");
            currennode.setContent("");
            if (currennode.getLevel() < totLevel) {
                //取消审批,给后一个人发
                List<CheckTree> tempchecker = checknodes.get(currennode.getLevel()).getNodes();
                if (tempchecker != null && tempchecker.size() > 0) {
                    for (int t = 0; t < tempchecker.size(); t++) {
                        if (!tempchecker.get(t).getName().equals(fromp) && !checkers.contains(tempchecker.get(t).getName())) {
                            System.out.println(fromp + "--------" + tempchecker.get(t).getName());
                            checkers.add(tempchecker.get(t).getName());
                        }
                    }
                }
            }
            type.delete02(currennode, "flowinfo");
            ToolKit.msgs_err("弃审成功");
            //dqcb.setErr(new ErrMessage("弃审成功"));
        } else {
            ToolKit.msgs_err("下一级已审核，不可进行修改");
            //dqcb.setErr(new ErrMessage("下一级已审核，不可进行修改"));
        }
        //sendMessage(fromp, checkers, title, message);
    }
}
